package workflow.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.hibernate.Query;
import org.springside.core.dao.HibernateEntityDao;

import workflow.model.ProcessInstance;
import workflow.util.XmlUtil;

import common.PageInfo;

public class ProcessInstanceDao extends HibernateEntityDao<ProcessInstance>{
	public List<ProcessInstance> findList(HashMap<String, String> hm, PageInfo pageInfo){
		try{
		
		String listHql = "FROM ProcessInstance po WHERE (1=1) ";
		String countHql = null;
		/*------------查询条件-------------*/
		//注意以设值方式组装查询条�?,参�?�PageItemManager 方法:findList
		
		String id = (String)hm.get("id");
		String proDefId = (String)hm.get("proDefId");
		String name = (String)hm.get("name");
		String processXml = (String)hm.get("processXml");
		String creator = (String)hm.get("creator");
		String createDate = (String)hm.get("createDate");
		String status = (String)hm.get("status");


		int count[] = {0, 0, 0, 0, 0, 0, 0};

		if(id != null && !"".equals(id.trim())){
			listHql += " AND id =:id";
			count[0] = 1;
		}

		if(proDefId != null && !"".equals(proDefId.trim())){
			listHql += " AND proDefId =:proDefId";
			count[1] = 1;
		}

		if(name != null && !"".equals(name.trim())){
			listHql += " AND name LIKE:name";
			count[2] = 1;
		}

		if(processXml != null && !"".equals(processXml.trim())){
			listHql += " AND processXml =:processXml";
			count[3] = 1;
		}

		if(creator != null && !"".equals(creator.trim())){
			listHql += " AND creator =:creator";
			count[4] = 1;
		}

		if(createDate != null && !"".equals(createDate.trim())){
			listHql += " AND createDate =:createDate";
			count[5] = 1;
		}

		if(status != null && !"".equals(status.trim())){
			listHql += " AND status LIKE:status";
			count[6] = 1;
		}

		listHql += " ORDER BY po.id DESC";
		countHql = listHql;
		countHql = countHql.replace("FROM ProcessInstance", "SELECT COUNT(*) FROM ProcessInstance");
		Query queryCount = this.createQuery(countHql);
		Query queryList = this.createQuery(listHql);

		if(count[0] == 1){
			queryList.setLong("id", Long.valueOf(id));
			queryCount.setLong("id", Long.valueOf(id));
		}

		if(count[1] == 1){
			queryList.setLong("proDefId", Long.valueOf(proDefId));
			queryCount.setLong("proDefId", Long.valueOf(proDefId));
		}

		if(count[2] == 1){
			queryList.setString("name", "%" + name + "%");
			queryCount.setString("name", "%" + name + "%");
		}

		if(count[3] == 1){
			queryList.setString("processXml", null);
			queryCount.setString("processXml", null);
		}

		if(count[4] == 1){
			queryList.setLong("creator", Long.valueOf(creator));
			queryCount.setLong("creator", Long.valueOf(creator));
		}

		if(count[5] == 1){
			queryList.setDate("createDate", (new java.text.SimpleDateFormat("yyyy-MM-dd")).parse(createDate));
			queryCount.setDate("createDate", (new java.text.SimpleDateFormat("yyyy-MM-dd")).parse(createDate));
		}

		if(count[6] == 1){
			queryList.setString("status", "%" + status + "%");
			queryCount.setString("status", "%" + status + "%");
		}



		//分页查询-START
		List countList = queryCount.list();
		int totalSize = 0;//总数
		if(countList.size() > 0){
			totalSize = Integer.valueOf(countList.get(0).toString());
			pageInfo.setTotalSize(totalSize);
		} else {
			pageInfo.setTotalSize(0);
		}
		
		queryList.setMaxResults(pageInfo.getPageSize());//设置查询数量
		queryList.setFirstResult(pageInfo.getPageNO() * pageInfo.getPageSize());//设置查询分段
		//分页查询-END
		
		return queryList.list();
		
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<ProcessInstance>();
		}
	}
	
	public boolean deleteByIds(String ids){
		boolean isSuccessful = false;
		try{			
			String hql = "DELETE FROM ProcessInstance po WHERE po.id IN (" + ids + ")";
			this.getHibernateTemplate().bulkUpdate(hql);
			isSuccessful = true;
		} catch (Exception e){
			e.printStackTrace();
		}
		
		return isSuccessful;
	}
	
}
